import { defineStore } from "pinia";
import { RouterConfigVO } from "@/entiy/vo/RouterConfigVO.ts";
import { VxeTablePropTypes } from "vxe-table";
import ArrayUtils from "@/utils/ArrayUtils.ts";
import PromiseUtils from "@/utils/PromiseUtils.ts";
import RequestService from "@/service/RequestService.ts";
import { RouterService } from "@/service/RouterService.ts";

export const useRouterManagerStore = defineStore("useRouterManagerStore", {
    state: () => {
        return {
            // 表格数据
            tableDataList: [] as Array<RouterConfigVO>,

            // 表格的加载状态
            isLoading: false,

            // 行配置
            rowConfig: {
                isCurrent: true,
                isHover: true,
                useKey: true,
                keyField: "id"
            } as VxeTablePropTypes.RowConfig,

            // 树形表格配置
            treeConfig: {
                // 平级数据展开菜单
                transform: true,
                // 展示连接线
                showLine: true,
                // 展开全部行
                expandAll: true,
                // 展开指定行 expandRowKeys: [10053]
                // 是否使用手风琴效果，只能展开一个节点
                accordion: false,
                // 树节点 ID 字段名
                rowField: 'id',
                // 树节点父 ID 字段名
                parentField: 'parentId'
            } as VxeTablePropTypes.TreeConfig,

            // 表格编辑配置
            editConfig: {
                // 手动模式编辑
                trigger: "manual",
                // 按行模式编辑
                mode: "row",
                // 显示是否已经编辑的状态
                showStatus: false,
                // 自动清除编辑状态
                autoClear: true
            } as VxeTablePropTypes.EditConfig,
        }
    },

    getters: {
        getMenuLinkText: () => (row: RouterConfigVO) => {
            if (row && row.isMenuLink == 1) {
                return "是";
            } else {
                return "否";
            }
        },

        getMenuLinkTagStatus: () => (row: RouterConfigVO) => {
            if (row && row.isMenuLink == 1) {
                return "primary";
            } else {
                return "error";
            }
        }
    },

    actions: {
        async getRouterConfigList(): Promise<void> {
            try {
                this.isLoading = true;
                const bizResult = await RouterService.getRouterConfigList();
                if (bizResult.isError) {
                    return PromiseUtils.reject(bizResult.errorMsg);
                }

                this.tableDataList = bizResult.bizDta ?? [];
                return PromiseUtils.resolve();
            } catch (e) {
                return PromiseUtils.rejectSystemError();
            } finally {
                this.isLoading = false;
            }
        },

        async updateRouterConfig(row: RouterConfigVO): Promise<void> {
            const bizResult = await RequestService.post<RouterConfigVO>("/update-router-config", row);
            if (bizResult.isError) {
                return PromiseUtils.reject(bizResult.errorMsg);
            }

            // 使用服务返回的数据更新列表
            const newItem: RouterConfigVO = bizResult.bizDta
            ArrayUtils.update(this.tableDataList, "id", newItem.id, newItem);
            return PromiseUtils.resolve();
        }
    }
})
